import { _decorator, Component, Node, UITransform, BoxCollider2D } from 'cc';
import { ObstacleManager } from './ObstacleManager';

const { ccclass, property } = _decorator;

/**
 * 视野障碍物
 * 
 * 功能：
 * 1. 定义视线阻挡对象
 * 2. 处理视野遮挡效果
 * 3. 提供障碍物属性配置
 * 4. 管理障碍物状态
 * 
 * 特点：
 * 1. 可配置的遮挡程度
 * 2. 动态障碍物支持
 * 3. 半透明障碍处理
 * 4. 多形状支持
 */
@ccclass('VisionObstacle')
export class VisionObstacle extends Component {
    @property
    public isOpaque: boolean = true;

    @property({ visible: function(this: VisionObstacle) { return !this.isOpaque; }})
    public visionBlockFactor: number = 0.5; // 0: 完全透明, 1: 完全阻挡

    @property
    public affectsShadow: boolean = true;

    start() {
        this.initCollider();
    }

    private initCollider() {
        // 使用 this.node 来获取组件
        let collider = this.node.getComponent(BoxCollider2D);
        if (!collider) {
            collider = this.node.addComponent(BoxCollider2D);
        }

        const transform = this.node.getComponent(UITransform);
        if (transform) {
            collider.size.width = transform.contentSize.width;
            collider.size.height = transform.contentSize.height;
        }

        collider.enabled = true;
        collider.sensor = true;
    }

    onEnable() {
        ObstacleManager.instance.addObstacle(this.node);
    }

    onDisable() {
        ObstacleManager.instance.removeObstacle(this.node);
    }

    public getBlockFactor(): number {
        return this.isOpaque ? 1 : this.visionBlockFactor;
    }
} 